<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Admin_banner extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('admin/admin_banner_model','admin/admin_pickup_model','admin/admin_counter_model','admin/admin_category_tree_model'));
		$this->load->library('pagination');
		//$this->admin_commoncall->check_admin_login();
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		//$this->template->set_partial('side_menu','layouts/side_menu');
		//$this->template->set_partial('nav_bar','layouts/nav_bar');

	}

	function add(){

		
		$data['mode'] = "ADD";
		$data['pickup']['banner_size'] = $this->admin_pickup_model->get_pickup(1);
		//$data['banners']['info'] = $this->admin_banner_model->get_banners();
		$data['parent']=  $this->admin_category_tree_model->get_cetegory();
		
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('admin/admin_banner',$data);
	}

	function edit($id){

		$data['mode'] = "EDIT";

		$data['banners']['details'] = $this->admin_banner_model->get_banner_details($id);
		$data['banner_size'] = $this->admin_pickup_model->get_pickup(1);
		$data['parent']=  $this->admin_category_tree_model->get_parent();
		
		$data['id'] = $id;
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('admin/admin_banner',$data);
	}

	function upload_banner()
	{

		if ($this->input->post('upload')) {


			//upload Photo and thumbnail Starts
			//Form -- TRUE
			// Banner Upload Configuration Start
			$filevariable=array_keys($_FILES);
			$userfile=$filevariable[0];
			$image=$_FILES[$userfile];

			$vSize = $this->input->post('banner_size');
			$vSize = explode('X',$vSize);
			$vH=0;
			$vW=0;

			$vW=$vSize[0];
			$vH=$vSize[1];


			$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/images/banner/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'prefix'=>'law_',				//format my_image_121326.jpg , if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'yes',
					'thumbwidth'=>$vW,
					'thumbheight'=>$vH,
					'thumbpath'=>'' 				// Dont write thumb folder name in path

			);
			//Banner upload Configure End
			$uploadData=$this->commoncall->upload($imageData);
			//$this->form_validation->set_rules('banner_title', 'banner_title', 'trim|required|xss_clean');
			//$this->form_validation->set_rules('impression', 'impression', 'trim|required|xss_clean');
			//$this->form_validation->set_rules('click', 'click', 'trim|required|xss_clean');
			//if($this->form_validation->run() == TRUE){
			
				$bannerdata=array(
					'file'=>$uploadData['data'],
					'title'=>$this->input->post('banner_title'),
					'size'=>$this->input->post('banner_size'),
					'link'=>$this->input->post('link'),
					'date_added'=>date('d-m-y',time()),
					'date_start'=>$this->input->post('start_date'),
					'date_end'=>$this->input->post('end_date'),
					'max_impressions'=>$this->input->post('impression'),
					'max_clicks'=>$this->input->post('click'),
					'shows'=>$this->input->post('show'),
					'category' =>$this->input->post('category')	
			);

			
				

			if ($uploadData['data'] == TRUE) {
				$this->db->insert('banner',$bannerdata);
					
					
				$this->session->set_flashdata('message', 'You have successful added new banner... ');
				redirect('banner/admin/admin_banner/add');
			//}		
			}else{
				$this->session->set_flashdata('message', 'Fields can not be null!... ');
				redirect('banner/admin/admin_banner/add');
			}
		} else{

			$this->session->set_flashdata('message', 'Please Choose a correct file!... ');
			redirect('banner/admin/admin_banner/add');
			exit();
		}
	}







	function update_banner()
	{

		if ($this->input->post('upload')) {


			//upload Photo and thumbnail Starts
			//Form -- TRUE
			// Banner Upload Configuration Start
			$filevariable=array_keys($_FILES);
			$userfile=$filevariable[0];
			$image=$_FILES[$userfile];

			$vSize = $this->input->post('banner_size');
			$vSize = explode('X',$vSize);
			$vH=0;
			$vW=0;

			$vW=$vSize[0];
			$vH=$vSize[1];


			$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/images/banner/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'prefix'=>'law_',				//format my_image_121326.jpg , if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'yes',
					'thumbwidth'=>$vW,
					'thumbheight'=>$vH,
					'thumbpath'=>'' 				// Dont write thumb folder name in path

			);
			//Banner upload Configure End
			$uploadData=$this->commoncall->upload($imageData);
			
			
			
			$id = $this->input->post('id');
			
			if($uploadData == TRUE){
			
			$bannerdata=array(
					'file'=>$uploadData['data'],
					'title'=>$this->input->post('banner_title'),
					'size'=>$this->input->post('banner_size'),
					'link'=>$this->input->post('link'),
					'date_added'=>date('d-m-y',time()),
					'date_start'=>$this->input->post('start_date'),
					'date_end'=>$this->input->post('end_date'),
					'max_impressions'=>$this->input->post('impression'),
					'max_clicks'=>$this->input->post('click'),
					'shows'=>$this->input->post('show'),
					'category'=>$this->input->post('category')
			);


			//--Delete Old Image File--//



			$sql = "Select file from banner where id=$id";
			$filename = $this->db->query($sql)->row()->file;
			$f = BASEPATH.'../assets/images/banner/'.$filename;
			unlink($f);
			
			
			$this->db->where('id',$id);
			$this->db->update('banner',$bannerdata);
			$this->session->set_flashdata('message', 'Banner Uploded Sucessfully!... ');
			redirect('banner/admin/admin_banner/edit/'.$this->input->post('id'));
			
			}else {
				$bannerdata=array(
						//'file'=>$uploadData['data'],
						'title'=>$this->input->post('banner_title'),
						'size'=>$this->input->post('banner_size'),
						'link'=>$this->input->post('link'),
						'date_added'=>date('d-m-y',time()),
						'date_start'=>$this->input->post('start_date'),
						'date_end'=>$this->input->post('end_date'),
						'max_impressions'=>$this->input->post('impression'),
						'max_clicks'=>$this->input->post('click'),
						'shows'=>$this->input->post('show')
				);
			}
			
			
			
			
			$this->db->where('id',$id);
			$this->db->update('banner',$bannerdata);
			$this->session->set_flashdata('message', 'Banner Updated Sucessfully!... ');
			redirect('banner/admin/admin_banner/edit/'.$this->input->post('id'));
			
			} else {
			$this->session->set_flashdata('message', 'Image or link is empty!... ');
			redirect('banner/admin/admin_banner/edit/'.$this->input->post('id'));
		}
			
	}

	function banner_list()
	{
		//-- Default Value --//
		$status=51;
		$featured=false;
		$search = "";
		$impression=NULL;
		$clicks=NULL;
		
		//$current_datetime=date_default_timezone_set('Africa/Lagos H:m:s');;
		$current_datetime=date('Y-m-d H:m:s');;
		$expire_datetime=24;
		$expire= NULL;

		if($this->input->post('filter')){
			$filter = $this->input->post('filter');


			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
				case 'impressions':
					$status = 51;
					$impression=1;
					//die('hhh');
					break;

				case 'clicks':
					$status = 51;
					$clicks=1;


					break;

				case 'expire':
					$status = 51;
					$expire=1;
					break;

			}
		}

		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_all_banner('banner');

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		
		$config['base_url'] = base_url() . 'banner/admin/admin_banner/banner_list';
		
		$cur =base_url()."banner/admin/admin_banner/banner_list";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		
		
		
		if($status==51)
		{
			$data['banners']['info'] = $this->admin_banner_model->get_banners($featured = $featured, $status = $status,$perpage, $offset, $impression=$impression, $clicks=$clicks, $expire=$expire);

			$this->pagination->initialize($config);
		}else{

			$status=1;
			$offset = 0 + $this->uri->segment(5);
			
			$total = $this->admin_counter_model->count_inactive_banner($status);

			//print "$total";
			//die();

			$perpage = 5;
			$config['per_page'] = $perpage;
			$config['total_rows'] = $total;
			$config['uri_segment'] = '5';

			
			$config['base_url'] = base_url() . 'banner/admin/admin_banner/inactive/';
			
			
			$cur =base_url()."banner/admin/admin_banner/inactive/";
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<div class="pagination"><ul>';
			$config['full_tag_close'] = '</ul></div><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			
			$data['banners']['info'] = $this->admin_banner_model->get_inactive_banners($featured = $featured, $status = $status,$perpage, $offset, $impression=$impression, $clicks=$clicks, $expire=$expire);

			$this->pagination->initialize($config);
		}

		//$data['banners']['info'] = $this->admin_banner_model->get_banners($featured = false, $status = $status, $no = 5, $offset = NULL, $impression=$impression, $clicks=$clicks, $expire=$expire);


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('admin/banner_list',$data);
	}
	function inactive()
	{
		//-- Default Value --//
		$status=1;
		$featured=false;
		$search = "";
		$impression=NULL;
		$clicks=NULL;
	//	$current_datetime=date('Y-m-d H:m:s');;
		$expire_datetime=24;
		$expire= NULL;

		if($this->input->post('filter')){
			$filter = $this->input->post('filter');


			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
				case 'impressions':
					$status = 51;
					$impression=1;
					//die('hhh');
					break;

				case 'clicks':
					$status = 51;
					$clicks=1;


					break;

				case 'expire':
					$status = 51;
					$expire=1;
					break;

			}
		}


		$status=1;
		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_inactive_banner($status);

		//print "$total";
		//die();

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		$config['base_url'] = base_url() . 'banner/admin/admin_banner/inactive/';
		
		$cur =base_url()."banner/admin/admin_banner/inactive/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		$data['banners']['info'] = $this->admin_banner_model->get_inactive_banners($featured = $featured, $status = $status,$perpage, $offset, $impression=$impression, $clicks=$clicks, $expire=$expire);

		$this->pagination->initialize($config);


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('admin/banner_list',$data);
	}


	

	function change_banner_status($status=NULL,$id=NULL) {
		$this->admin_banner_model->change_status($id,$status);
		redirect('banner/admin/admin_banner/banner_list');
	}



	//-- Banner Delete (Subha)--//
	function delete($id,$image_file) {
		$this->admin_banner_model->delete_banner($id,$image_file);
		redirect('banner/admin/admin_banner/banner_list');
	}

}